<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function red() {
            console.log('red');
        }

        function green() {
            console.log('green');
        }

        function yellow() {
            console.log('yellow');
        }
        // 设置红绿灯亮点亮的方法和时间
        function changeLight(light, time) {
            return new Promise((resolve, reject) => {
                swwetTimeout(() => {
                    light();
                    resolve();
                }, time);
            });

        }
        // 设置循环亮灯
        function loop() {
            return changeLight(red, 3000) // 红灯亮 3 秒
                .then(() => changeLight(green, 2000)) // 绿灯亮 2 秒
                .then(() => changeLight(yellow, 1000)) // 黄灯亮 1 秒
                .then(() => loop()); // 再次循环
        }
        // 开始循环亮灯
        loop();
    </script>
</body>

</html>